package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    Scanner sc = new Scanner(System.in);
    TeacherService teacherService = new TeacherService();

    public void start() {
        System.out.println("==========欢迎来到《老师》管理系统==========");

        teacherLoop:
        while (true) {
            System.out.println("请输入您要进行的操作：");
            System.out.println("1、添加老师\t2、删除老师\t3、修改老师\t4、查看老师\t5、退出程序！");
            String choice = sc.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用，再见！");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误！请重新输入！");
                    break;
            }
        }

    }

    private void updateTeacher() {
        String id = inputTeacherId();

        Teacher teacher = inputTeacherInfo(id);

        teacherService.updateTeacher(teacher, id);
        System.out.println("修改成功！");
    }

    private void deleteTeacher() {
        String id = inputTeacherId();

        teacherService.deleteTeacherById(id);
        System.out.println("删除成功！");

    }

    private String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入老师ID：");
            id = sc.next();
            boolean result = teacherService.isExists(id);
            if (!result) {
                System.out.println("查无此人！请重新输入！");
            } else {
                break;
            }
        }
        return id;
    }

    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("查无信息！请添加后重试！");
            return;
        }
        System.out.println("ID\t\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }

    private void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师id");
            id = sc.next();
            boolean result = teacherService.isExists(id);
            if (result) {
                System.out.println("信息已经存在，请重新输入");
            } else {
                break;
            }
        }

        Teacher teacher = inputTeacherInfo(id);

        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
